/*
 * Class:PDayView
 * 
 * Description: This class represent the day view for the calendar.
 * 
 * Version: 2.0 - Mathieu Canuel - 26-11-2011
 * 		Initial implementation
 * 
 */
package calendarInterface2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import calendarEventManager.CEMAgenda;
import calendarEventManager.CEMEvent;
import calendarEventManager.CEMListColor;
import controller.CTLEngine;

@SuppressWarnings("serial")
public class PDayView extends JPanel{
	//GUI Components
	JPanel pHeader;
	JPanel pDate;
	JPanel pNavigation;
	JPanel pDayView;
	JPanel pHour;
	JPanel pHoursEvent;
	JLabel lDate;
	JLabel[] lHours;
	JButton bPrevious;
	JButton bNext;
	CalendarButton[] button;
	int year = Calendar.getInstance().get(Calendar.YEAR);
	int day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
	int hour = 0;

	public PDayView() {
		super();
		build();
	}
	public PDayView(Calendar calendar) {
		super();
		this.day = calendar.get(Calendar.DAY_OF_YEAR);
		this.year = calendar.get(Calendar.YEAR);
		build();
	}

	public void build(){
		setLayout(new BorderLayout());
		setSize(new Dimension(780,568));
		setVisible(true);

		pHeader = new JPanel(new BorderLayout());
		pNavigation = new JPanel();
		pDate = new JPanel();
		pHeader.add(pDate, BorderLayout.CENTER);
		pHeader.add(pNavigation, BorderLayout.EAST);

		pDayView = new JPanel(new BorderLayout());
		pDayView.setPreferredSize(new Dimension(780,568));
		pHour = new JPanel(new GridLayout(0,1));
		pHour.setSize(new Dimension(24,68));
		pHoursEvent = new JPanel(new GridLayout(0,1));
		pHoursEvent.setSize(new Dimension(824,568));
		pDayView.add(pHour,BorderLayout.WEST);
		pDayView.add(pHoursEvent);

		addButtons();
		addLabels();
		displayDate();
		add(pHeader,BorderLayout.NORTH);
		add(pDayView, BorderLayout.SOUTH);
	}

	//**************************************************************
	// Method: displayDate
	// Description: Display the calendar informations on the panel
	//**************************************************************
	public void displayDate()  {
		SimpleDateFormat sdf = new SimpleDateFormat("EEEE, d MMM yyyy",Locale.ENGLISH);
		for (int x = 0; x < button.length; x++){
			button[x].setText("");
			button[x].setBackground(Color.white);
		}
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.DAY_OF_YEAR, day);
		lDate.setText(sdf.format(cal.getTime()));
		ShowEvent(cal);
	}

	@SuppressWarnings("deprecation")
	private boolean compareTimestamp(Timestamp date, Timestamp startingTime) {
		Timestamp endingTime = new Timestamp (startingTime.getTime());
		endingTime.setHours(endingTime.getHours()+1);
		return date.equals(startingTime) || (date.after(startingTime) && date.before(endingTime));
	}

	public void addButtons(){
		button = new CalendarButton[24];
		for (int x = 0; x < button.length; x++) {
			button[x] = new CalendarButton(null);
			button[x].setFocusPainted(false);
			button[x].setBackground(Color.white);
			pHoursEvent.add(button[x]);
		}
		bPrevious = new JButton("<");
		bPrevious.addActionListener(new ActionPreviousDay());
		bNext = new JButton(">");
		bNext.addActionListener(new ActionNextDay());
		pNavigation.add(bPrevious);
		pNavigation.add(bNext);
	}

	public void addLabels(){
		lDate = new JLabel("", JLabel.CENTER);
		lHours = new JLabel[24];
		for (int x = 0; x < button.length; x++) {
			lHours[x] = new JLabel(hour + ":00",JLabel.CENTER);
			lHours[x].setBackground(Color.white);
			hour++;
			pHour.add(lHours[x]);
		}
		pDate.add(lDate);
		Font font = new Font("Arial",Font.BOLD,16);
		lDate.setFont(font);
	}

	@SuppressWarnings("deprecation")
	public void ShowEvent(Calendar cal){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (int x = 0; x < button.length; x++){
			button[x].setDate(Timestamp.valueOf(sdf.format(cal.getTime()) + " " + x + ":00:00"));
			button[x].addActionListener(new ActionAddEvent(button[x].getDate()));
			CEMListColor liste = new CEMListColor();
			for(CEMAgenda oAgenda : CTLEngine.getAllAgendas()) {

				Color color = CalendarInterface.getColor(liste.get(oAgenda.getAgendaColour()));
				if(oAgenda.isActivated()) {
					for (CEMEvent oEvent : CTLEngine.getAllEvents(oAgenda.getAgendaId())) {
						if(compareTimestamp(oEvent.getStartingTime(),button[x].getDate()) ) {
							int duree = oEvent.getEndingTime().getHours()-oEvent.getStartingTime().getHours();
							for(int i = x; i<duree+x+1; i++) {
								for( ActionListener al : button[i].getActionListeners() ) {
									button[i].removeActionListener( al );
								}

								button[i].setBackground(color);
								button[i].setForeground(Color.white);
								button[i].addActionListener(new ActionModifyEvent(oEvent));
							}
							button[x].setText(oEvent.getName() + " " + new Time(oEvent.getStartingTime().getTime()) + 
									" - " + new Time(oEvent.getEndingTime().getTime()));

						}
					}

				}}
		}
	}
	//Actions des boutons
	public class ActionPreviousDay implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			day--;
			hour = 0;
			displayDate();
			setSideCalendar();
		}
	}

	public class ActionNextDay implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			day++;
			hour = 0;
			displayDate();
			setSideCalendar();
		}
	}
	
	private void setSideCalendar(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.DAY_OF_YEAR, day);
		CalendarInterface.setSideCalDate(cal);
	}

	//Retourne la date courrante
	public Calendar getCalendar() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.DAY_OF_YEAR, day);
		return cal;
	}

	public class ActionAddEvent implements ActionListener{
		Timestamp date;
		public ActionAddEvent(Timestamp date) {
			this.date = date;
		}
		
		public void actionPerformed(ActionEvent e) {
			CalendarInterface.showAddEventView(date);
		}
	}

	public class ActionModifyEvent implements ActionListener{
		CEMEvent oEvent;
		public ActionModifyEvent(CEMEvent oEvent) {
			this.oEvent = oEvent;
		}
		public void actionPerformed(ActionEvent e) {
			CalendarInterface.showUpdateEventView(oEvent);
		}
	}
}